{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13c59917-785e-4c45-89b2-cab15b38cf6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Dumping model to file cache C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.421 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精确模式 研究生/在/研究/生命/的/起源\n",
      "全模式 研究/研究生/生在/研究/研究生/生命/的/起源\n",
      "搜索引擎模式: 研究/研究生/在/研究/生命/的/起源\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "seg_list=jieba.cut(\"研究生在研究生命的起源\",cut_all=False)\n",
    "print(\"精确模式\",\"/\".join(seg_list))\n",
    "seg_list=jieba.cut(\"研究生在研究生命的起源\",cut_all=True)\n",
    "print(\"全模式\",\"/\".join(seg_list))\n",
    "seg_list=jieba.cut_for_search(\"研究生在研究生命的起源\")\n",
    "print(\"搜索引擎模式:\",\"/\".join(seg_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "270d4440-89f7-4f8e-9f30-8067d3518ad2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未删除停用词前的10个高频词：[('，', 59), ('的', 32), ('。', 22), ('、', 17), ('冬奥会', 16), ('\\n', 16), ('北京', 15), ('中国', 13), ('会徽', 12), ('年', 11)]\n",
      "删除停用词前的10个高频词：[('冬奥会', 16), ('北京', 15), ('中国', 13), ('会徽', 12), ('年', 11), ('冬', 8), ('2022', 7), ('运动员', 7), ('冰雪', 6), ('中心', 5)]\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "def TF_word(words, topk):\n",
    "    data = {}\n",
    "    for k in words:\n",
    "        if k in data:\n",
    "            data[k] += 1\n",
    "        else:\n",
    "            data[k] = 1\n",
    "    data = sorted(data.items(), key=lambda x:x[1], reverse=True)\n",
    "    return data[:topk]\n",
    "file_content = open(r'C:\\Users\\Administrator\\Desktop\\dongaohui.txt',encoding='utf-8').read()\n",
    "split_words = jieba.lcut(file_content)\n",
    "print(\"未删除停用词前的{0}个高频词：{1}\".format(10,TF_word(split_words,10)))\n",
    "stop_words = [line.strip() for line in open(r'C:\\Users\\Administrator\\Desktop\\stops_list.txt','r',encoding='utf-8').readlines()]\n",
    "stop_words.append('\\n')\n",
    "split_words2 = [x for x in split_words if x not in stop_words]\n",
    "print(\"删除停用词前的{0}个高频词：{1}\".format(10,TF_word(split_words2,10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32da7e6d-c8c3-42af-a58a-9f222157f56b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
